
/**
 * 指定一个ESP8266驱动WS2812灯带
 * 利用SPI实现，但只能使用D7引脚
 * @param {device} device 目标ESP8266开发板
 * 
 */
 function WS2812(device){
     /**
      * 
      * @param {number} r 红 
      * @param {number} g 绿
      * @param {number} b 蓝
      */
    
    var command = new Command()
    var colors = ""
    /**
     * 
     * @param {number} r 255色值
     * @param {number} g 255色值
     * @param {number} b 255色值
     */
    this.add = function(r,g,b){
       var R = this.append(r.toString(2))
       var G = this.append(g.toString(2))
       var B = this.append(b.toString(2))
       for(i in R){
         if(i>=8)break 
         colors+=R[i]
       }

       for( i in G){
        if(i>=8)break 
        colors+=G[i]
      }

      for( i in B){
        if(i>=8)break 
        colors+=B[i]
      }
    return this
    }


    this.clear = function(){
      command = new Command()
      colors = ""
      return this
    }

    this.show=function(){
      command.add("SPI.WS2812="+colors+";")
      sendCommand(command.build(),device)
      return this
    }
    this.append = function(what){
      var left = 8-what.length
      var end =  what
      for(var i = 0 ; i<left;i++){
          end = "0"+end
      }
      return end
    }

    sendCommand("SPI.begin")
    sendCommand("SPI.setBitOrder=1")
    sendCommand("SPI.setClockDivider=1")
}

